<?php

namespace app\admin\model\air;

use think\Model;
use app\admin\model\air\OfficialBuilding;
use app\admin\model\Area;


class Airport extends Model
{

    

    

    // 表名
    protected $name = 'air_airport';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'info_time_text',
        'status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['normal' => __('Status normal'), 'hidden' => __('Status hidden')];
    }


    public function getInfoTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['info_time']) ? $data['info_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setInfoTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    
    public function building()
    {
        return $this->hasMany(OfficialBuilding::class, 'air_airport_id', 'id');
    }

    public function portarea()
    {
        return $this->belongsTo(Area::class, 'area_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

}
